﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace PB.Airsoft.Core.Utilities.Validation
{
    public class Guard
    {
        public static void ThrowIfNegative(int value, string message, string paramName)
        {
            ThrowIfLessThan(value, 0, message, paramName);
        }

        public static void ThrowIfLessThan(int value, int minValue, string message, string paramName)
        {
            if (value < minValue)
                throw new ArgumentOutOfRangeException(paramName, message);
        }

        public static void ThrowIfNull(object obj)
        {
            if (obj == null)
                throw new ArgumentNullException();
        }

        public static void ThrowIfNull(object obj, string paramName)
        {
            if (obj == null)
                throw new ArgumentNullException(paramName);
        }

        public static void ThrowIfNull(object obj, string paramName, string message)
        {
            if (obj == null)
                throw new ArgumentNullException(paramName, message);
        }

        public static void ThrowIfNullOrContainsNull(IEnumerable collection, string paramName)
        {
            ThrowIfNull(collection, paramName);
            if (collection.Cast<object>().Contains(null))
                throw new ArgumentNullException(paramName);
        }

        public static void ThrowIfEmptyString(string str)
        {
            if (string.IsNullOrEmpty(str))
                throw new ArgumentException();
        }

        public static void ThrowIfEmptyString(string str, string message)
        {
            if (string.IsNullOrEmpty(str))
                throw new ArgumentException(message);
        }

        public static void ThrowIfEmptyString(string str, string paramName, string message)
        {
            if (string.IsNullOrEmpty(str))
                throw new ArgumentException(message, paramName);
        }

        public static void ThrowIfEmpty<T>(IEnumerable<T> collection, string paramName, string message)
        {
            if (collection == null)
                throw new ArgumentNullException(paramName, message);

            if (!collection.Any())
                throw new ArgumentException(message, paramName);
        }

        public static void ThrowIfLonger(string str, int maxLength, string paramName)
        {
            if (!string.IsNullOrEmpty(str) && str.Length > maxLength)
                throw new ArgumentOutOfRangeException(paramName, String.Format("{0} cannot be longer than {1} characters.", paramName, maxLength));
        }

        public static void ThrowIfNotEqual<T>(T value1, T value2, string message)
        {
            if (!EqualityComparer<T>.Default.Equals(value1, value2))
                throw new ArgumentException(message);
        }

        public static void ThrowIfEmptyGuid(Guid guid, string message)
        {
            if (guid == Guid.Empty)
                throw new ArgumentException(message);
        }
    }
}
